<?php

namespace App\Repositories;

use App\Interfaces\ShopService;
use App\Models\Shop;

class ShopRepository implements ShopService
{


    public function get($shop_id)
    {
        return Shop::find($shop_id);

    }

    public function getFitterGoodsPaginate($shop_id,$stock=0,$pageSize=15,$orderType=''){

        $searchQuery = Shop::select('*');
        //关键词


        $searchQuery = $searchQuery->rightJoin('goods','shop.id','=','goods.shop_id');
        $searchQuery = $searchQuery->whereNull('goods.deleted_at');


        $searchQuery = $searchQuery->where('goods.is_on_sale',1);
        $searchQuery = $searchQuery->where('goods.shop_id',$shop_id);




        //筛选是否有货
        if ($stock==1){
            $searchQuery = $searchQuery->where('goods.goods_number','>',0);
        }

        $searchQuery = $searchQuery->groupBy('goods.goods_id');

        //销量排序 类型: sale_desc|sale_asc
        if ($orderType=='sale_desc'){
            $searchQuery = $searchQuery->orderBy('sales_num','DESC');
        }elseif($orderType=='sale_asc'){
            $searchQuery = $searchQuery->orderBy('sales_num','ASC');
        }

        //价格排序 类型:price_desc|price_asc
        if ($orderType=='price_desc'){
            $searchQuery = $searchQuery->orderBy('shop_price','DESC');
        }elseif($orderType=='price_asc'){
            $searchQuery = $searchQuery->orderBy('shop_price','ASC');
        }

        //默认 时间倒序
        $searchQuery = $searchQuery->orderBy('add_time','desc');


        $searchResult = $searchQuery->paginate($pageSize);


        return $searchResult;
    }


}